<?php

	if (PHP_SAPI != 'cli') {
		session_start();
	}

	define('ROOT_PATH', __DIR__);

	require_once(__DIR__ . '/config/defines.php');
	require_once(SYSTEM_PATH . '/Init.php');
	require_once(SYSTEM_PATH . '/Utils.php');

	if (DEBUG_MODE) {
		ini_set('display_errors', 'on');
		error_reporting(E_ALL);
	}

	$module = empty($_REQUEST['do']) ? DEFAULT_MODULE : $_REQUEST['do'];

	try {
		$loader = new System_Loader($module);
		echo $loader->output;

		if (DEBUG_MODE && PHP_SAPI == 'cli') {
			echo "\n########\n";
			foreach ($loader->debug_info as $k => $v) {
				echo "{$k}: {$v}\n";
			}
			echo "Memory usage: " . memory_get_usage(true) . "\n";
		}

	} catch (AppException $e) {
		echo '<b>Error message:</b> ' . $e->getMessage() . '<br/>';
		echo '<b>Trace:</b><br/><pre>';
		echo $e->getTraceAsString();
		echo '</pre>';
	}



